package cn.sunxyz.code.original;

import static cn.sunxyz.code.boolean_.BooleanAlgebra.*;

/**
 * Created by yangrd on 2017/3/21.
 */
public final class Original {

    /**
     * 全加器 《编码》 p141
     * Created by sunxyz on 2017/3/21.
     */
    public static HalfAdder fullAdder(boolean x, boolean y, boolean carry) {
        HalfAdder halfAdder1 = halfAdder(x, y);//当前位 x+y 得到和与 进位
        HalfAdder halfAdder2 = halfAdder(halfAdder1.sum, carry); //将当前位得到的和与上一位相加得到的进位进行相加 得到和与进位
        boolean carry1 = or(halfAdder1.carry, halfAdder2.carry);//是否进位
        boolean sum1 = halfAdder2.sum;//当前位运算的和与进位和
        return new HalfAdder(sum1, carry1);
    }

    public static HalfAdder halfAdder(boolean x, boolean y) {
        return new HalfAdder(xor(x, y), and(x, y));
    }

    /**
     * 半加器
     * Created by sunxyz on 2017/3/21.
     */
    public static class HalfAdder {
        public final boolean sum;
        public final boolean carry;

        public HalfAdder(boolean sum, boolean carry) {
            this.sum = sum;
            this.carry = carry;
        }
    }

    public static boolean[] oneByte() {
        return new boolean[8];
    }
}
